

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:untitled1/app/res/myColors.dart';
import 'package:untitled1/app/utils/utilRouter.dart';
import 'package:untitled1/app/utils/utilScreen.dart';
import 'package:untitled1/app/widgets/MyContainer.dart';
import 'package:untitled1/app/widgets/button/MyElevatedButton.dart';
import 'package:untitled1/app/widgets/toast/MyToast.dart';

import '../../../../../res/myColors.dart';
import '../../../../../utils/utilRouter.dart';
import '../../../../../utils/utilScreen.dart';
import '../../../../../widgets/MyContainer.dart';
import '../../../../../widgets/toast/MyToast.dart';
import '../../../../../bean/KeyValueBean.dart';
import 'EmployerPage_workTypeSelectController.dart';
import 'EmployerPage_workTypeSelectController2.dart';
//工种筛选
class EmployerPage_workTypeSelect2 extends StatelessWidget {
  final Function( List<KeyValueBean> list)? onConfirm; // 确认按钮回调（可选）
  EmployerPage_workTypeSelect2({super.key, this.onConfirm});
  final controller = Get.put(EmployerPage_workTypeSelectController2());

  @override
  Widget build(BuildContext context) {
    return Obx(()=>Container(
      padding: const EdgeInsets.only(top: 10, left: 15, right: 15),
      child: Stack(
        alignment: Alignment.topCenter,
        children: [
          Align(
            alignment: Alignment(0, -1),
            child: SingleChildScrollView(
              child: Container(
                width: utilScreen.getScreenWidth(context),
                margin: EdgeInsets.only(top: 40),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text('请选择辅助工种',style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold,color: myColors.c_191919),),
                    SizedBox(height: 5,),
                    getGridView(controller.list_gz),
                  ],
                ),
              ),
            ),
          ),
          Align(
            alignment: Alignment(0, 0),
            child: Stack(
              children: [
                Align(
                  alignment: Alignment.topCenter,
                  child: Text(
                    '全部筛选',
                    style: TextStyle(
                      color: myColors.c_191919,
                      fontSize: 16,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
                Align(
                  alignment: Alignment.topRight,
                  child:GestureDetector(child:  Text('关闭'),onTap: () {
                    utilRouter.back();
                  },),
                )
              ],
            ),
          ),
          Align(
            alignment: Alignment(0, 1),
            child: Container(
              width: utilScreen.getScreenWidth(context) - 50,
              height: 50,
              margin: EdgeInsets.only(bottom: 20),
              decoration: BoxDecoration(
                color: Colors.red,
                borderRadius: BorderRadius.circular(50),
              ),
              child: Row(
                children: [
                  Expanded(
                    flex: 1,
                    child: GestureDetector(
                      onTap: () {
                        print(">]aaaaaa");
                        controller.clearData();
                      },
                      child: Container(
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                          color: myColors.c_F89596, // 背景色
                          borderRadius: BorderRadius.only(
                            topLeft: Radius.circular(50), // 左上圆角
                            bottomLeft: Radius.circular(50), // 左下圆角
                            topRight: Radius.zero, // 右上直角
                            bottomRight: Radius.zero, // 右下直角
                          ),
                        ),
                        child: Text(
                          '重置',
                          style: TextStyle(
                            color: Colors.white,
                            fontSize: 14,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                      ),
                    ),

                  ),
                  Expanded(
                    flex: 1,
                    child: GestureDetector(
                      onTap: () {
                        // print(">]aaaaaa");

                        if (controller.select().length==0) {
                          MyToast.showToast(context,"请选择");
                          return;
                        }
                        if (onConfirm != null) {
                          onConfirm!(controller.select());
                        }
                        utilRouter.back();
                      },
                      child: Container(
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                          color: myColors.c_FF4142, // 背景色
                          borderRadius: BorderRadius.only(
                            topLeft: Radius.zero, // 左上圆角
                            bottomLeft: Radius.zero, // 左下圆角
                            topRight: Radius.circular(50), // 右上直角
                            bottomRight: Radius.circular(50), // 右下直角
                          ),
                        ),
                        child:Text(
                          '确定',
                          style: TextStyle(
                            color: Colors.white,
                            fontSize: 14,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                      ),
                    ),

                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    ));
  }

  Widget getGridView(RxList<KeyValueBean> list) {
    return GridView.builder(
      shrinkWrap: true, // ❗ 自动根据内容高度收缩
      physics: NeverScrollableScrollPhysics(), // ❗ 禁止自身滚动
      padding: const EdgeInsets.all(1),
      // 整体内边距
      gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 3, // 一行 3 个 item
        crossAxisSpacing: 5, // 横向间距
        mainAxisSpacing: 5, // 纵向间距
        childAspectRatio: 2, // 宽高比，<1 高的更多
      ),
      itemCount: list.length,
      // 总共 30 项
      scrollDirection: Axis.vertical,

      // ❗ 禁止自身滚动
      itemBuilder: (context, index) {
        var bean=list[index];
        return MyContainer(
          onTap: () {
            // onConfirm!(list[index]);
            if(bean.ui_select_view_data==1){
              bean.ui_select_view_data=0;
            }else{
              bean.ui_select_view_data=1;
            }
            print(">] ${bean.ui_select_view_data}");
            controller.list_gz.refresh();
          },
          height: 40,
          alignment: Alignment.center,
          decoration: BoxDecoration(
            color:bean.ui_select_view_data==1?myColors.c_F89596:myColors.c_F4F5F6,
            borderRadius: BorderRadius.circular(8),
          ),
          child: Text("${list[index].value}"),
        );
      },
    );
  }
}
